package com.huatai.model.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huatai.common.api.R;
import com.huatai.common.constant.KafkaConstant;
import com.huatai.model.entity.ModelEntity;
import com.huatai.model.service.ModelService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author Lion
 * @date 2024/4/7  11:12
 */
@RequestMapping("/model")
@RestController
@Api(value = "模型管理",tags = "模型管理")
public class ModelController{

	@Autowired
	protected ModelService service;

	@Autowired
	private KafkaTemplate<String, Object> kafkaTemplate;

	@ApiOperation(value = "获取信息-单个",
		notes = "直接返回实体，未通过ResponseMessage封装")
	@ApiImplicitParam(name = "id", required = true, value = "数据主键")
	@PostMapping("/get")
	public R<ModelEntity> get(@RequestParam(name = "id") Long id) throws Exception {
		ModelEntity data = service.getById(id);
		if (data == null) {
			return R.fail("获取失败，数据记录[{}]不存在");
		}
		service.buildExInfo(data);
		return R.data(data);
	}

	@ApiOperation(value = "获取数据-分页",
		notes = "采用Mybatis方式")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "data", value = "请求参数字典，具体取值如下", dataType = "Map"),
		@ApiImplicitParam(name = "data.modelName", value = "模型名称", dataType = "String"),
		@ApiImplicitParam(name = "data.typeKey", value = "分类", dataType = "String"),
		@ApiImplicitParam(name = "data.useKey", value = "用途", dataType = "String"),
		@ApiImplicitParam(name = "data.industryKey", value = "行业", dataType = "String"),
		@ApiImplicitParam(name = "data.label", value = "标签", dataType = "String"),
		@ApiImplicitParam(name = "data.pageIndex", value = "当前页码"),
		@ApiImplicitParam(name = "data.pageSize", value = "每页大小")
	})
	@PostMapping(value = "/page")
	public R<IPage<ModelEntity>> page(@RequestBody JSONObject data) {
		IPage<ModelEntity> page = service.pageByRequestMap(data);
		service.buildExInfo(page.getRecords());
		return R.data(page);
	}

	@ApiOperation(value = "保存数据",
		notes = "当ID存在时，则更新，否则新增")
	@PostMapping(value = "/save")
	public R save(@ApiParam(required = true) @RequestBody ModelEntity data) {
		service.SaveOrUpdateWithValid(data);
		return R.success("保存成功");
	}

	@ApiOperation(value = "获取数据-列表",
		notes = "采用Mybatis方式")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "data", value = "请求参数字典，具体取值如下", dataType = "Map"),
		@ApiImplicitParam(name = "data.modelName", value = "模型名称", dataType = "String"),
		@ApiImplicitParam(name = "data.typeKey", value = "分类", dataType = "String"),
		@ApiImplicitParam(name = "data.useKey", value = "用途", dataType = "String"),
		@ApiImplicitParam(name = "data.industryKey", value = "行业", dataType = "String"),
		@ApiImplicitParam(name = "data.label", value = "标签", dataType = "String")
	})
	@PostMapping(value = "/list")
	public R<List<ModelEntity>> list(@RequestBody JSONObject data) {
		List<ModelEntity> list = service.list(data);
		service.buildExInfo(list);
		return R.data(list);
	}

	@ApiOperation(value = "删除数据",
		notes = "根据主键批量删除，不验证主键是否存在")
	@ApiImplicitParam(name = "data", value = "数据主键", required = true,paramType = "list")
	@PostMapping("/delete")
	public R delete(@RequestBody List<Long> data) {
		service.removeByIds(data);
		//saas服务监听修改内容
		JSONObject mdendit = new JSONObject();
		mdendit.put("md_id",data.get(0));
		//endit_type类型1为修改2删除
		mdendit.put("edit_type",2);
		kafkaTemplate.send(KafkaConstant.MODEL_EDIT_INFO,mdendit.toJSONString());
		return R.success("success");
	}

	@ApiOperation(value = "删除数据",
		notes = "根据主键删除，不验证主键是否存在")
	@DeleteMapping("/delete/{id}")
	public R delete(@PathVariable("id") Long id) {
		service.removeById(id);
		return R.success("success");
	}


}
